﻿$(document).ready(function() {
    
    $('#button-find').click(function() {
        loadData(1);
    });
    loadData(1);

    $(document).on('click', '.grid .btn.remove', function () {
        var $this = $(this);
        $.prompt("Do you want to remove this authorized?", {
            title: "Confirm",
            buttons: { "Yes": true, "Cancel": false },
            submit: function(e, v) {
                if (!v) return;
                var result = authorizedImplement.deleteItem($this.attr('rel'));
                if (result) {
                    $this.closest('tr').remove();
                } else {
                    $.prompt('Delete this authorized failed.');
                }
            }
        });
    });
    
});

var status = 'pending';
var currentPage, pageNum;

function loadData(pageNum, isPaged) {
    var result = authorizedImplement.getList(pageNum);

    if (result != null) {
        $('#list').setTemplateURL("/templates/authorized-list.htm");
        $('#list').processTemplate(result);

        if (!isPaged) {
            paginator(result.RowCount, result.PageSize);
        }
    }

    currentPage = pageNum;
}

function paginator(rowCount, pageSize) {
    $('#pager').smartpaginator({
        totalrecords: rowCount,
        recordsperpage: pageSize,
        length: 5,
        next: 'Next',
        prev: 'Prev',
        first: 'First',
        last: 'Last',
        theme: 'blue',
        controlsalways: true,
        onchange: function(newPage) {
            loadData(newPage, true);
            currentPage = newPage;
            $('#r').html('Page # ' + newPage);
        }
    });
}

var authorizedImplement = new Object(
    {
        getList: function(pageNum) {
            var list;
            $.ajax({
                type: "POST",
                url: "/Pages/AuthorizationList.aspx/GetAuthorizedList",
                data: JSON.stringify({ pageNum: pageNum }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(data) {
                    list = data.d;
                },
                error: function() {
                    list = null;
                }
            });

            return list;
        },
        deleteItem: function (id) {
//            alert(id);
            var result;
            $.ajax({
                type: "POST",
                url: "/Pages/AuthorizationList.aspx/DeleteItem",
                data: JSON.stringify({ id: id }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function (data) {
                    result = data.d;
                },
                error: function () {
                    result = false;
                }
            });

            return result;
        }
    });